package com.shycloud.mido.music.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.music.domain.*;
import com.shycloud.mido.music.dto.MisMusicLibraryDTO;
import com.shycloud.mido.music.mapper.*;
import com.shycloud.mido.music.service.MisMusicLibraryService;
import com.shycloud.mido.music.util.CommonString;
import com.shycloud.mido.music.vo.CollCourseWareVO;
import com.shycloud.mido.music.vo.CourseWareMusicIPageVO;
import com.shycloud.mido.music.vo.MusicLibraryVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 曲库模块service实现类
 *
 * @author nianhua.jiang
 * @date 2020/4/27 15:37
 */
@Service
@AllArgsConstructor
public class MisMusicLibraryServiceImpl extends ServiceImpl<MisMusicLibraryMapper, MisMusicLibrary> implements MisMusicLibraryService {

	/** 曲库模块 mapper */
	private MisMusicLibraryMapper misMusicLibraryMapper;
	/** 分类曲库关系 mapper */
	private MisCateLibMapper misCateLibMapper;
	/** 曲集模块 mapper */
	private MisMusicCollectionMapper musicCollectionMapper;
	/** 曲库-曲集关系 mapper */
	private MisLibCollMapper misLibCollMapper;
	/** 曲目模块 mapper */
	private MisMusicMapper musicMapper;


	/**
	 * 通过一级分类ID获取曲库列表
	 *
	 * @param cateId
	 * @return java.util.List<com.shycloud.mido.music.vo.MusicLibraryVO>
	 * @author nianhua.jiang
	 * @date 2020/4/27 15:38
	 */
	@Override
	public List<MusicLibraryVO> getLibraryByCateId(Integer cateId) {
		return null;
	}

	/**
	 * 【添加/编辑】曲库
	 *
	 * @param misMusicLibraryDTO 【添加/编辑】曲库 DTO
	 * @return boolean
	 * @author nianhua.jiang
	 * @date 2020/4/29 17:35
	 */
	@Override
	@Transactional(rollbackFor = {Exception.class})
	public boolean saveMusicLibrary(MisMusicLibraryDTO misMusicLibraryDTO) {
		//在新增的时候判断是否有重复的曲库名称
		if (misMusicLibraryDTO.getId() == null) {
			QueryWrapper queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("name", misMusicLibraryDTO.getName());
			if (super.list(queryWrapper).size() != 0) {
				return false;
			}
		}
		//更新/新增曲库表
		MisMusicLibrary misMusicLibrary = new MisMusicLibrary();
		BeanUtil.copyProperties(misMusicLibraryDTO, misMusicLibrary);

		if (super.saveOrUpdate(misMusicLibrary)) {

			//获得分类Id的list集合
			List<Integer> cateIdList = misMusicLibraryDTO.getCateId();
			//无论新增还是修改都先清空【分类-曲库】关系表
			misCateLibMapper.delete(new QueryWrapper<MisCateLib>().eq(MisCateLib.COL_LIBRARY_ID, misMusicLibraryDTO.getId()));
			//添加【分类-曲库】关系表
			cateIdList.forEach(x -> {
				//初始化【曲库分类关系表】
				MisCateLib misCateLib = new MisCateLib();
				//分类ID
				misCateLib.setCategoryId(x);
				//曲库ID
				misCateLib.setLibraryId(misMusicLibrary.getId());
				//新增
				misCateLibMapper.insert(misCateLib);
			});
		}
		return true;
	}

	/**
	 * 分页查询曲库列表
	 *
	 * @param misMusicLibrary
	 * @return com.baomidou.mybatisplus.core.metadata.IPage
	 * @author nianhua.jiang
	 * @date 2020/4/30 10:25
	 */
	@Override
	public IPage getMusicLibrary(MisMusicLibraryDTO misMusicLibrary) {

		//********** 最近前端工程狮比较忙，不想让前端动代码，所以这部分只能麻烦点，望维护者见谅 *********/
		//获取分页参数
		Page page = new Page(misMusicLibrary.getCurrent(), misMusicLibrary.getSize());

		//获取曲库信息 (get(0)是迫不得已，望见谅)
		IPage<MusicLibraryVO> musicLibrary = misMusicLibraryMapper.getMusicLibrary(page, misMusicLibrary.getCateId().get(0));

		List<MusicLibraryVO> musicLibraryVOList = musicLibrary.getRecords();

		for (MusicLibraryVO musicLibraryVO : musicLibraryVOList) {

			//查询曲库-曲集关系
			List<MisLibColl> misLibColls = misLibCollMapper.selectList(new QueryWrapper<MisLibColl>()
					.eq(MisLibColl.COL_LIBRARY_ID, musicLibraryVO.getId())
					.and(x -> x.eq(MisLibColl.COL_DEL_FLAG, CommonString.DELETE_FLG)));

			//获取曲集ID
			List<Integer> collectionList = misLibColls.stream().map(MisLibColl::getConllectionId).collect(Collectors.toList());

			Iterator<Integer> iterator = collectionList.iterator();

			//定义曲目数量
			int musicCount = 0;

			//查询曲集信息，筛选掉删除和禁用的
			while (iterator.hasNext()) {

				Integer collectionId = iterator.next();

				MisMusicCollection musicCollection = musicCollectionMapper.selectById(collectionId);
				if ("1".equals(musicCollection.getCollectionStatus()) || CommonString.DELETE_FLG_DEL
						.equals(musicCollection.getDelFlag())) {
					iterator.remove();
					continue;
				}

				QueryWrapper<MisMusic> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq(MisMusic.COL_DEL_FLAG, CommonString.DELETE_FLG);
				queryWrapper.eq(MisMusic.COL_COLLECTION_ID, collectionId);
				queryWrapper.eq(MisMusic.COL_STATUS, "0");
				queryWrapper.eq(MisMusic.COL_FILE_STATUS, "1");

				Integer count = musicMapper.selectCount(queryWrapper);
				musicCount += count;

			}
			musicLibraryVO.setMusicCount(musicCount);
		}

		return musicLibrary;
	}

	@Override
	public IPage getMusicLibraryMini(Page page, Integer cateId) {

		//获取曲库信息
		IPage<MusicLibraryVO> musicLibrary = misMusicLibraryMapper.getMusicLibrary(page, cateId);

		List<MusicLibraryVO> musicLibraryVOList = musicLibrary.getRecords();

		for (MusicLibraryVO musicLibraryVO : musicLibraryVOList) {

			//查询曲库-曲集关系
			List<MisLibColl> misLibColls = misLibCollMapper.selectList(new QueryWrapper<MisLibColl>()
					.eq(MisLibColl.COL_LIBRARY_ID, musicLibraryVO.getId())
					.and(x -> x.eq(MisLibColl.COL_DEL_FLAG, CommonString.DELETE_FLG)));

			//获取曲集ID
			List<Integer> collectionList = misLibColls.stream().map(MisLibColl::getConllectionId).collect(Collectors.toList());

			Iterator<Integer> iterator = collectionList.iterator();

			//定义曲目数量
			int musicCount = 0;

			//查询曲集信息，筛选掉删除和禁用的
			while (iterator.hasNext()) {

				Integer collectionId = iterator.next();

				MisMusicCollection musicCollection = musicCollectionMapper.selectById(collectionId);
				if ("1".equals(musicCollection.getCollectionStatus()) || CommonString.DELETE_FLG_DEL
						.equals(musicCollection.getDelFlag())) {
					iterator.remove();
					continue;
				}

				QueryWrapper<MisMusic> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq(MisMusic.COL_DEL_FLAG, CommonString.DELETE_FLG);
				queryWrapper.eq(MisMusic.COL_COLLECTION_ID, collectionId);
				queryWrapper.eq(MisMusic.COL_STATUS, "0");
				queryWrapper.eq(MisMusic.COL_FILE_STATUS, "1");

				Integer count = musicMapper.selectCount(queryWrapper);
				musicCount += count;

			}
			musicLibraryVO.setMusicCount(musicCount);
		}
		return musicLibrary;

	}


	@Override
	public IPage<CourseWareMusicIPageVO> getLibraryAndCollList(Page page, Integer cateId) {

		//先查询对应分类的曲库
		IPage<CourseWareMusicIPageVO> musicLibraryPages = misMusicLibraryMapper.getMusicLibraryForCourseware(page, cateId);

		List<CourseWareMusicIPageVO> musicLibrarys = musicLibraryPages.getRecords();

		for (CourseWareMusicIPageVO musicLibrary : musicLibrarys) {

			//根据曲库ID搜索曲集
			List<CollCourseWareVO> musicCollections = musicCollectionMapper.getMusicConllectionCourseware(musicLibrary.getLibId());

			musicLibrary.setCollections(musicCollections);

		}

		musicLibraryPages.setRecords(musicLibrarys);

		return musicLibraryPages;
	}
}